package com.example.hello.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.hello.entity.DeptDO;
import com.example.hello.model.PageRequest;
import com.example.hello.model.PageResult;
import com.example.hello.model.Result;
import com.example.hello.service.DeptService;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result<List<DeptDO>> getAllDepts() {
        return Result.success(deptService.getAllDepts());
    }
    
    @GetMapping("/page")
    public Result<PageResult<DeptDO>> getDeptPage(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageRequest request = new PageRequest();
        request.setName(name);
        request.setPageNum(page);
        request.setPageSize(pageSize);
        return Result.success(deptService.getDeptPage(request));
    }
    
    @GetMapping("/{id}")
    public Result<DeptDO> getDeptById(@PathVariable Integer id) {
        return Result.success(deptService.getDeptById(id));
    }
    
    @PostMapping
    public Result<Void> createDept(@RequestBody DeptDO dept) {
        deptService.createDept(dept);
        return Result.success(null);
    }
    
    @PutMapping("/{id}")
    public Result<Void> updateDept(@PathVariable Integer id) {
        deptService.updateDept(id);
        return Result.success(null);
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteDept(@PathVariable Integer id) {
        deptService.deleteDept(id);
        return Result.success(null);
    }
} 